package village.rainbow.slipin.adapter;

import java.util.List;

import village.rainbow.slipin.R;
import village.rainbow.slipin.datamodel.DataRow;
import village.rainbow.slipin.uicontrol.BaseArrayAdapter;
import village.rainbow.slipin.uicontrol.ResizeLayout;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class ListFolderAdapter extends BaseArrayAdapter {

	private static int LAYOUT = R.layout.list_folder;
	private LayoutInflater mInflater;
	
	private OnClickListener mListener;
	
	public ListFolderAdapter(Context context, List<DataRow> objects, ResizeLayout resizeLayout) {
		super(context, LAYOUT, objects, resizeLayout);
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		
		if(convertView == null){
			convertView = mInflater.inflate(LAYOUT, null);
		}
		
		final DataRow bean = (DataRow) getItem(position);
		String folderId = bean.getString("FOLDER_ID", "");
		String folderNm = bean.getString("FOLDER_NM", "");
		
		Button btnFolder = (Button)convertView.findViewById(R.id.toggle_folder);
		btnFolder.setText(folderNm);
		btnFolder.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				if(mListener != null)
					mListener.onClick(v);
				toggleOther(position);
			}
		});
		
		// TODO : 버튼 아이콘 바꿔야함
		// All
		if(folderId.equals("-1")){}
		// Setting
		else if(folderId.equals("-99")){}
		else {}
		
		boolean isSelected = (Boolean) bean.getTag("IS_SELECTED", false);
		if(isSelected == false){
			btnFolder.setBackgroundColor(Color.WHITE);
		}else{
			btnFolder.setBackgroundColor(Color.rgb(50, 50, 50));
		}
		
		return convertView;
	}
	
	public void setOnClickListener(OnClickListener listener){
		mListener = listener;
	}
	
	private void toggleOther(int position){
		for(int i=0; i<this.getCount(); i++){
			DataRow row = (DataRow) this.getItem(i);
			row.setTag("IS_SELECTED", i == position ? true : false);
		}
		this.notifyDataSetChanged();
	}
	
}